const api = require('../../config/supplier.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showProp:false,
    mainActiveIndex: 0,
    activeId: [],
    activeKey: '',
    supplierAll: [],
    supplierRight: '',
    imgUrls: [],
    imgUrl: 'https://p3.pstatp.com/large/43700001e49d85d3ab52',
    width: '',
    height: ''
  },
  getRightList() {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      imgUrls: []
    })
    wx.request({
      url: api.rightList, //仅为示例，并非真实接口地址。
      data: {
        classifyId: this.data.activeKey
      },
      method: 'GET',
      success: (res) => {
        wx.hideLoading()
        if (res.data.code === 100) {
          let obj = res.data.object
          // let imgs = obj.map(item => {return item.advertisementImage})
          this.setData({
            imgUrls: obj
          })
        } else if (res.data.code === 200) {
          wx.showToast({
            title: res.data.msg
          });
        }

      },
      fail:()=>{
        wx.hideLoading()
      }
    });
  },
  onChange(e) {
    this.getRightList()
  },
  myClick(e) {
    const {
      id
    } = e.target.dataset
    this.setData({
      activeKey: id
    })
  },
  onClickNav({
    detail = {}
  }) {
    this.setData({
      mainActiveIndex: detail.index || 0
    })
  },

  valided(){
		this.setData({
			showProp:false
		})
    let hasUser = getApp().hasUser()
		if(!hasUser){
			this.setData({
				showProp: true
      })
      return false
		}else{
			this.setData({
				showProp: false
      })
      return true
		}
  },

  onClickItem({
    detail = {}
  }) {
    const {
      activeId
    } = this.data

    const index = activeId.indexOf(detail.id)
    if (index > -1) {
      activeId.splice(index, 1)
    } else {
      activeId.push(detail.id)
    }

    this.setData({
      activeId
    })
  },
  goDetail(e) {
    if(!this.valided()){
      return
    }
    const {
      tag
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/supplierDetail/index?id=${tag}`
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    getApp().userInfoReadyCallback = res => {
      _this.setData({
        showProp: true
      })
    }
    this.setData({
      width: wx.getStorageSync('windowWidth'),
      height: wx.getStorageSync('windowHeight')
    })
  },
  getLeftList() {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: api.leftList, //仅为示例，并非真实接口地址。
      data: {},
      method: 'GET',
      success: (res) => {
        wx.hideLoading()
        if (res.data.code === 100) {
          let obj = res.data.object
          // let imgs = obj.map(item => {return item.advertisementImage})
          this.setData({
            supplierAll: obj
          })
          this.setData({
            activeKey: obj[0].id
          })
          this.getRightList()
        } else if (res.data.code === 200) {
          wxuni.showToast({
            title: res.data.msg
          });
        }

      },
      fail:()=>{
        wx.hideLoading()
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getLeftList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})